package org.yagnus.stats.samplers;

import java.util.List;

/**
 * 
 * @author hc.busy
 */
public abstract class RandomNumberGeneratorShuffle implements
		RandomNumberGenerater {

	// Perform in place shuffle on array
	public <X> void shuffle(X[] arr) {
		for (int i = 0; i < arr.length; ++i) {
			int nextInd = i + 1 + nextInt(arr.length - i);

			// perform swap
			X t = arr[i];
			arr[i] = arr[nextInd];
			arr[nextInd] = t;
		}
	}

	// Perform in place shuffle on array
	public <X> void shuffle(List<X> arr) {
		final int sz = arr.size();
		for (int i = 0; i < sz - 1; ++i) {
			int nextInd = i + nextInt(sz - i);

			// perform swap
			X t = arr.get(i);
			arr.set(i, arr.get(nextInd));
			arr.set(nextInd, t);
		}
	}

}
